package com.lh.music.fragment;


import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.lh.music.activity.LocalSongActivity;
import com.lh.music.R;
import com.lh.music.activity.SongDetailActivity;
import com.lh.music.adapter.MusicFragmentListAdapter;
import com.lh.music.dao.SongDao;
import com.lh.music.datamanager.DataManager;
import com.lh.music.entity.MusicFragmentEntity;
import com.lh.music.entity.Song;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class MusicFragment extends Fragment {
    private View view;
    private SwipeRefreshLayout refreshLayout;
    private ListView listView;
    private List<MusicFragmentEntity>  mData=new ArrayList<>();
    private MusicFragmentListAdapter listAdapter;
    //本地全部歌曲
    private List<Song> songList=new ArrayList<>();
    //我喜欢的歌曲
    private List<Song> favorateSongList=new ArrayList<>();
    //对数据库进行的操作
    private SongDao dao;
    public MusicFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view=inflater.inflate(R.layout.fragment_music, container, false);
        refreshLayout= (SwipeRefreshLayout) view.findViewById(R.id.srfl);
        listView= (ListView) view.findViewById(R.id.listView);
        refreshLayout.setColorSchemeColors(getResources().getColor(R.color.refresh)
                ,getResources().getColor(R.color.topBarTextSelect));
        //刷新事件
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Toast.makeText(getActivity(), "刷新成功", Toast.LENGTH_SHORT).show();
                new MyTask().execute();
                refreshLayout.setRefreshing(false);
            }
        });
        initData();
        dao=new SongDao(getActivity());
        listAdapter=new MusicFragmentListAdapter(getActivity(),mData);
        listView.setAdapter(listAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        Intent intent = new Intent(getActivity(), LocalSongActivity.class);
                        startActivity(intent);
                        break;
                    case 4:
                        Intent intent1=new Intent(getActivity(), SongDetailActivity.class);
                        intent1.putExtra("flag","favorate");
                        Bundle bundle=new Bundle();
                        bundle.putSerializable("songList", (Serializable) favorateSongList);
                        intent1.putExtra("bundle",bundle);
                        startActivity(intent1);
                        break;
                }
            }
        });
        new MyTask().execute();
        return view;
    }

    private void initData() {
        String [] name={"本地音乐","最近播放","下载管理","我的歌手","我喜欢听"};
        String [] count={"（0）","（0）","（0）","（0）","（0）"};
        int [] imgId={R.mipmap.a5v,R.mipmap.a5t,R.mipmap.a5u,R.mipmap.a5s,R.mipmap.a5y};
        for (int i=0;i<5;i++){
            MusicFragmentEntity music=new MusicFragmentEntity();
            music.setCount(count[i]);
            music.setName(name[i]);
            music.setImgId(imgId[i]);
            mData.add(music);
        }

    }
    class MyTask extends AsyncTask<Void,Void,Integer>{


        @Override
        protected Integer doInBackground(Void... params) {
            DataManager dataManager=new DataManager(getActivity());
            songList.clear();
            favorateSongList.clear();
            songList.addAll(dataManager.findAllSong());
            //将本地音乐全部存到表中
            dao.addSongList(songList);
            favorateSongList.addAll(dao.findAllFavorateSong());
            return null;
        }

        @Override
        protected void onPostExecute(Integer integer) {
            mData.get(0).setCount("("+dao.findAllSong().size()+")");
            mData.get(4).setCount("("+favorateSongList.size()+")");
            listAdapter.notifyDataSetChanged();
        }
    }
}


